package com.test.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class DoFormAction
 */
public class DoFormAction extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DoFormAction() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		boolean b = isTokenValid(request);
		
		if(!b) {
			System.out.println("请不要重复提交!");
			return;
		}
		
		request.getSession().removeAttribute("token");
		
		String username = request.getParameter("username");
		System.out.println("add username==>" + username);
	}

	private boolean isTokenValid(HttpServletRequest request) {
		
		String clientToken = request.getParameter("token");
		if(clientToken == null) {
			return false;
		}
		
		String serverToken = (String) request.getSession().getAttribute("token");
		if(serverToken == null) {
			return false;
		}
		
		if(!clientToken.equals(serverToken)) {
			return false;
		}
		
		return true;
	}

}
